package com.alifi.crm.web.home.screen;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.buc.sso.client.util.SimpleUserUtil;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.common.lang.StringUtil;
import com.alibaba.common.logging.Logger;
import com.alibaba.common.logging.LoggerFactory;
import com.alibaba.platform.buc.sso.common.dto.SimpleSSOUser;
import com.alifi.crm.biz.guess.Base;
import com.alifi.crm.biz.guess.EmployeeGuessManager;
import com.alifi.crm.biz.guess.GuessMessage;
import com.alifi.crm.dal.enu.States;
import com.alifi.crm.dal.object.EmployeeGuessDO;

/**
 * 
 * @author wenjing.huangwj
 *
 */
public class Guess {

	@Resource(name="employeeGuessManager")
	EmployeeGuessManager employeeGuessManager;
	
	protected static final Logger      logPermission = LoggerFactory.getLogger("uums-permission-logger");	
	protected static final Logger      logger = LoggerFactory.getLogger(Guess.class);	
	
	
	public void execute(HttpServletRequest request, HttpServletResponse response,Navigator nav,final Context ctx){
		try {
			if(StringUtil.equalsIgnoreCase((String)ctx.get("action"), "true")){   //如果经过action，直接返回
				return;
			}
			SimpleSSOUser user = SimpleUserUtil.findUser(request);   // 姓名: lastName   花名: nickNameCn 
			if((user==null)||StringUtil.isBlank(user.getEmpId())){   //检查
				ctx.put("error", GuessMessage.NOT_GET_LOGIN_INFO);
				return;
			}
			ctx.put("user", user);
			
		     //为employee_num增加机会
			addLimitTimes(request.getParameter("employee_num"),user.getEmpId());
		    
		    EmployeeGuessDO guessDO = employeeGuessManager.getEmployeeGuess(StringUtil.trim(user.getEmpId()));
			
		    if(guessDO==null){
	            ctx.put("error", GuessMessage.RIGHT_AMOUNT_IS_NULL);
	            logPermission.warn("数据库里没有额度数据，empId=" + user.getEmpId() +", lastName="+ user.getLastName() + ", nickNameCn=" + user.getNickNameCn());
	            return;
	         }
		    
		    if(States.准备.equals(guessDO.getStates())){
		    	employeeGuessManager.comeIn(guessDO.getEmployeeNum());
		    }
		    
		    Base.checkAndSetError(guessDO,ctx,employeeGuessManager);
		    ctx.put("active_times",  Base.toStringSubtract(guessDO.getLimitGuessTimes(),guessDO.getGuessTimes()));
		    ctx.put("guessDO", guessDO);
		} catch (Exception e) {
			ctx.put("error", e.getMessage());
			logger.error(e);  
		} 
	}
	
	
	private void addLimitTimes(String limitTimesEmployeeNum,String recipient){
		if(StringUtil.isNotBlank(limitTimesEmployeeNum)&&(!StringUtil.equalsIgnoreCase(limitTimesEmployeeNum, recipient))){
	    	EmployeeGuessDO limitTimesEmployeeGuessDO = employeeGuessManager.getEmployeeGuess(limitTimesEmployeeNum);
	    	
	    	if(limitTimesEmployeeGuessDO!=null){ 
	    		employeeGuessManager.addLimitGuessTimes(limitTimesEmployeeGuessDO, StringUtil.trim(recipient));
	    	}
	    }
	}

}
